#include "Headers.h"
#include <fstream>
using namespace std;

void Clear_Screen(int H,int D);
void Set_Cursor_XY(int x,int y);
void Set_Cursor_XYA(COORD C);
COORD Get_Cursor_XY();
HWND OpenWindow(const char* cszWinClassName, const char* cszWindowName,  int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	//Open the window
	HWND hWnd = OpenWindow("Watch out for loaders", "KA-BOOM!",nCmdShow);

	MSG uMsg;
	memset ( &uMsg, 0, sizeof ( uMsg ) );
	
	while(uMsg.message != WM_QUIT)
	{
		while(PeekMessage(&uMsg,NULL,0,0,PM_REMOVE))
		{
			TranslateMessage(&uMsg);
			DispatchMessage(&uMsg);
		}
#ifdef _VERSION1_
	
#ifdef _DEBUGGING_
		//Clock C;
	//ofstream out;
	//BlockGrid * G;
	//C.Init();
	//double AverageTime = 0;
	//double TotalTime = 0,Hold = 0;
	//int Runs = 0;
	//out.open("Testing Files/Data2.txt",ios::out);
	////while(Runs < 1000)
	////{
	//	G = new BlockGrid();
	//	G->SetStorageArea("Testing Files/Data.txt");
	//	G->FillGrid();
	//	//Hold = C.Run();
	//	//TotalTime += Hold;
	//	//AverageTime += (Hold/(++Runs));
	//	//if(!(Runs % 1000)){
	//	//	out << "Average Time: " << AverageTime << endl;
	//	//	out	<< "Run: " << Runs << endl;
	//	//}
	//	G->SaveToFile();
	//	G->ClearGrid();
	//	delete G;
	////}

	//out << "Average Time: " << AverageTime << endl;
	//out << "Total Time(secs) : " << TotalTime;
	//out.close();
	//
	///*G.FillGrid();
	//G.SaveToFile();
	//G.FillGrid();

	//for(int i = 0;i < GRID_SIZE;++i)
	//{
	//	for(int j = 0;j < GRID_SIZE;++j)
	//	{
	//		printf("%i ",G.GetBlock(j,i).GetTypeFlag());
	//	}
	//	putchar('\n');
	//}*/
		
#endif
#endif
	}
	_Console C;
	World * W = new World("Test map");
	C.Print("Success");
	_getch();
	return uMsg.wParam;
}

void Clear_Screen(int H,int D)
{
	int i,j;
	Set_Cursor_XY(0,0);
	if(D == 80)
	{
		for(i = 0;i < H;++i)
		{
			printf("                                                                                ");
		}
	}
	else
	{
		for(i = 0;i < H;++i)
		{
			for(j = 0;j < D;++j)
				printf(" ");
			if(D < 80)
				printf("\n");
		}
	}
	Set_Cursor_XY(0,0);
}

void Set_Cursor_XY(int x,int y)
{
	COORD XY;
	XY.X = x;
	XY.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), XY);
}

void Set_Cursor_XYA(COORD C)
{
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), C);
}

COORD Get_Cursor_XY()
{
	PCONSOLE_SCREEN_BUFFER_INFO SBInfo = NULL;
	COORD C;
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), SBInfo);
	C.X = SBInfo->dwCursorPosition.X;
	C.Y = SBInfo->dwCursorPosition.Y;
	return C;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_KEYDOWN:
		switch(wParam)
		{
		case VK_ESCAPE:
			PostQuitMessage(0);
			break;
		}
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

HWND OpenWindow ( const char* cszWinClassName, const char* cszWindowName, int nCmdShow )
{
	WNDCLASSEX wc;
	HWND hWnd;

	HINSTANCE hInstance = ::GetModuleHandle ( NULL );
	assert ( NULL != hInstance );

	wc.lpszClassName	= cszWinClassName;
	wc.cbSize			= sizeof ( WNDCLASSEX );
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= WindowProc;
	wc.hInstance		= hInstance;
	wc.hIcon			= NULL;
	wc.hIconSm			= NULL;
	wc.hCursor			= ::LoadCursor ( NULL, IDC_ARROW );
	wc.hbrBackground	= ( HBRUSH )::GetStockObject( BLACK_BRUSH );
	wc.lpszMenuName		= NULL;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;

	if ( 0 == RegisterClassEx ( &wc ) )
	{
		return NULL;
	}

	hWnd = ::CreateWindowEx ( NULL,
		cszWinClassName,
		cszWindowName,
		WS_OVERLAPPEDWINDOW,
		0, 0,
		SCREEN_WIDTH, SCREEN_HEIGHT,
		NULL, NULL, ::GetModuleHandle ( NULL ), NULL );
	ShowWindow ( hWnd, nCmdShow );
	UpdateWindow ( hWnd );
	return hWnd;
}